<?php
class Backend_News_Controller extends Backend_Controller_Crud_Vc{

     public function listAction()
     {
            $pager = Request::post('pager', 'array', array());
            $filter = Request::post('filter', 'array', array());
            $query = Request::post('search', 'string', false);
               
            $dataModel = Model_News::getInstance();
            $vc = Model_Vc::getInstance();
            
            $fields = array('id' ,'title','news_date','published' ,'published_version','date_created','date_updated');
        
            $data = $dataModel->getListVc($pager , $filter, $query, $fields,'user','updater' );
                
            if(empty($data))
                Response::jsonSuccess(array() ,array('count'=>0));
        
            $ids = array(); 
            foreach ($data as $k=>$v)
                $ids[] = $v['id'];
            
            $maxRevisions = $vc->getLastVersion('news',$ids);
            
            foreach ($data as $k=>&$v)
                if(isset($maxRevisions[$v['id']]))
                    $v['last_version'] = $maxRevisions[$v['id']];
                else
                    $v['last_version'] = 0;   

            Response::jsonSuccess($data, array( 'count'=>$dataModel->getCount($pager , $filter , $query)));
     }
     
    public function yearlistAction(){
        $model = Model_News::getInstance();
        $years = $model->getYears();
        $result = array();
        $result[] = array('id'=>'','title'=>'All');
        if(!empty($years)){
            foreach ($years as $k=>$v)
                $result[] = array('id'=>$v,'title'=>$v);
        }
        Response::jsonSuccess($result);
    }
}